import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);
const mqtt = require('mqtt');
const mqttTopic =
  'b5e1726951d82efb3ab70a18e0de4d6b9a760aaed0c70a57895a758aa50279cf';
export default new Vuex.Store({
  state: {
    mqttClient: null,
    mqttMessage: null,
    mqttBaseTopic: null,
    mqttConnectionStatus: 0, // mqtt连接状态状态机
    mqttMsgType: null, //mqtt消息回执中的confType
    mqttMsgMethod: null, //mqtt消息回执中的method
  },
  getters: {
    mqttMessage: function(state) {
      return state.mqttMessage;
    },
    mqttBaseTopic: function(state) {
      return state.mqttBaseTopic;
    },
    mqttClient: function(state) {
      return state.mqttClient;
    },
    mqttConnStat: function(state) {
      return state.mqttConnectionStatus;
    },
    mqttMsgType: function(state) {
      return state.mqttMsgType;
    },
    mqttMsgMethod: function(state) {
      return state.mqttMsgMethod;
    },
  },
  mutations: {
    /**
     * 判断mqtt是否连接
     */
    CHANGE_MQTT_CONN_STAT: function(state, newStatus) {
      if (newStatus === 0 || newStatus === 1 || newStatus === 2) {
        state.mqttConnectionStatus = newStatus;
      } else {
        state.mqttConnectionStatus = newStatus;
      }
    },
    CONNECTMQTT: function(state, event) {
      state.mqttClient = event;
    },
    PUBLISH_LAST(state, event) {
      state.mqttClient.publish(`${mqttTopic}/AnimalTag/query/lastData`, event);
    },
    PUBLISHMSG(state, event) {
      state.mqttClient.publish(
        `${mqttTopic}/AnimalTag/query/historyData`,
        event
      );
    },
    PUBLISH_VERSION(state, event) {
      state.mqttClient.publish(`${mqttTopic}/AnimalTag/query/tagInfo`, event);
    },
    SUBSCRIPTION(state) {
      state.mqttClient.subscribe(`${mqttTopic}/AnimalTag/lastData`);
      state.mqttClient.subscribe(`${mqttTopic}/AnimalTag/historyData`);
      state.mqttClient.subscribe(`${mqttTopic}/AnimalTag/tagInfo`);
      state.mqttClient.subscribe(`${mqttTopic}/AnimalTag/data`);
    },
    SUBMESSAGE(state, event) {
      state.mqttMessage = null;
      state.mqttMessage = event.message;
      state.mqttBaseTopic = event.topic;
    },

    SAVE_MQTT_CLIENT: function(state, client) {
      state.mqttClient = client;
    },

    MQTT_MSG_HANDLER: function(state, msgObj) {
      try {
        let receivedMsgObj = JSON.parse(msgObj.message);
        state.mqttMsgType = receivedMsgObj.confTyp;
        state.mqttMsgMethod = receivedMsgObj.method;
        state.mqttMessage = receivedMsgObj;
      } catch (error) {
        window.console.log(error);
      }
    },

    SAVE_DEBUG_MQTT_CLIENT: function(state, client) {
      state.debugmqttClient = client;
    },

    MQTT_DEBUGMSG_HANDLER: function(state, msgObj) {
      try {
        let receivedMsgObj = JSON.parse(msgObj.message);
        state.debugmqttMsg = receivedMsgObj;
      } catch (error) {
        window.console.log(error);
      }
    },
  },
  actions: {
    connectMqttClient: function({ commit }, server) {
      commit('CHANGE_MQTT_CONN_STAT', 2);
      const mqttClient = mqtt.connect('wss://yigrow.cn/mqtt/', {
        username: 'shhooserver001',
        password: 'shhoo2018',
        keepalive: 55,
      });
      mqttClient.on('connect', function() {
        commit('CHANGE_MQTT_CONN_STAT', 1); //状态为1
        commit('CONNECTMQTT', mqttClient);
        commit('SUBSCRIPTION');
        // commit(server.con, mqttClient); //连接服务器
        // if (server.con === 'SAVE_MQTT_CLIENT') {
        //   window.console.log('Os mqtt service connection success!');
        // } else if (server.con === 'SAVE_DEBUG_MQTT_CLIENT') {
        //   window.console.log('Debug mqtt service connection success!');
        // }
      });
      mqttClient.on('message', (topic, message) => {
        commit('SUBMESSAGE', {
          message: message.toString(),
          topic: topic,
        });
      });
    },
  },
});
